package com.zmm.myyimixian.ui.activity.main.home;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseActivity;
import com.zmm.myyimixian.bean.HomeBoosTitle;
import com.zmm.myyimixian.ui.activity.main.home.childFragment.ChildFragment;
import com.zmm.myyimixian.ui.adapter.Find_tab_Adapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeBoosActivity extends BaseActivity {

    @BindView(R.id.boos_fh_iv)
    ImageView boosFhIv;
    @BindView(R.id.boos_tb)
    Toolbar boosTb;
    @BindView(R.id.boss_tm_tl)
    TabLayout bossTmTl;
    @BindView(R.id.boss_child_vp)
    ViewPager bossChildVp;
    private MyHandler handler = new MyHandler(this);

    @Override
    protected void initView() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        new MyHomeBoosTitleThread(handler, id).start();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_boos;
    }


    @OnClick(R.id.boos_fh_iv)
    public void onClick() {
        finish();
    }

    //获取网上下载的数据
    private class MyHandler extends Handler {

        private WeakReference<HomeBoosActivity> reference = null;

        public MyHandler(HomeBoosActivity activity) {
            reference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            HomeBoosActivity activity = reference.get();
            if (activity != null) {
                if (msg.what == 1) {
                    List<HomeBoosTitle> list = (List<HomeBoosTitle>) msg.obj;
                    Find_tab_Adapter adapter = new Find_tab_Adapter(getSupportFragmentManager(), addFragments(list), list);
                    bossChildVp.setAdapter(adapter);
                    bossTmTl.setupWithViewPager(bossChildVp);
                }
            }
        }
    }

    private List<Fragment> addFragments(List<HomeBoosTitle> list) {
        List<Fragment> mFragment = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            HomeBoosTitle boosTitle = list.get(i);
            mFragment.add(ChildFragment.getBundle(boosTitle.getId()));
        }
        return mFragment;
    }
}
